package com.zkaler.commons.utils.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by khiker on 2016/6/21.
 */
@Slf4j
public class PropertiesLoader {
    //默认属性配置文件文件名
    private static final String DEFAULT_PROPERTY = "properties/config";
    private static final String PROPERTY_SUFIX = ".properties";
    //配置文件缓存Map
    private static final Map<String, PropertiesConfiguration> CONFIGURATIONS = new HashMap<String, PropertiesConfiguration>();

    /**
     * 获得配置文件实例
     *
     * @param name 配置文件路径
     * @return
     */
    private static PropertiesConfiguration getInstance(String name) {
        String fileName = getPropertyName(name);
        PropertiesConfiguration config = CONFIGURATIONS.get(fileName);
        if (config == null) {
            try {
                config = new PropertiesConfiguration(fileName);
                //配置文件更改自动重载
                config.setReloadingStrategy(new FileChangedReloadingStrategy());
                CONFIGURATIONS.put(fileName, config);
            } catch (ConfigurationException e) {
                log.error("cannot find property file for : " + name);
                throw new RuntimeException("cannot find property file for : " + name);
            }
        }
        return config;
    }

    //获得配置文件全名（包含扩展名）
    private static String getPropertyName(String name) {
        if (StringUtils.isBlank(name)) {
            name = DEFAULT_PROPERTY;
        }
        return name.endsWith(PROPERTY_SUFIX) ? name : (name += PROPERTY_SUFIX);
    }

    /**
     * 从默认配置文件获得配置
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getString(DEFAULT_PROPERTY, key);
    }

    /**
     * 根据配置文件名与key获得配置
     *
     * @param propertyInstance
     * @param key
     * @return
     */
    public static String getString(String propertyInstance, String key) {
        return getInstance(propertyInstance).getString(key);
    }

    /**
     * 根据key获得默认配置文件long类型配置
     *
     * @param key
     * @return
     */
    public static Long getLong(String key) {
        return getLong(DEFAULT_PROPERTY, key);
    }

    /**
     * 根据配置文件名与key获取long类型的配置
     *
     * @param propertyInstance
     * @param key
     * @return
     */
    public static Long getLong(String propertyInstance, String key) {
        return getInstance(propertyInstance).getLong(key);
    }

    /**
     * 根据key获取默认配置文件int类型的配置
     *
     * @param key
     * @return
     */
    public static Integer getInt(String key) {
        return getInt(DEFAULT_PROPERTY, key);
    }

    /**
     * 根据配置文件名与key获取int类型的配置
     *
     * @param propertyInstance
     * @param key
     * @return
     */
    public static Integer getInt(String propertyInstance, String key) {
        String value = getInstance(propertyInstance).getString(key);
        return value == null ? null : Integer.parseInt(value);
    }

    /**
     * 根据配置文件名与key获取boolean类型的配置
     *
     * @param key
     * @param propertyInstance
     * @param defaults         默认值
     * @return
     */
    public static boolean getBoolean(String propertyInstance, String key, boolean defaults) {
        return getInstance(propertyInstance).getBoolean(key, defaults);
    }
}
